<?php
namespace app\controller\api\diy;


use app\FrontendController;
use think\facade\Request;

use app\model\tool\ImageModel;
use app\model\cms\ArticleModel;

use app\model\frontend\SettingModel;
use app\model\frontend\DiyModel;
use app\model\frontend\CategoryModel;
use app\model\frontend\GoodsModel;
class Index extends FrontendController{

    private $diy_id = 0;
    public function single( Request $request ){

        // global $_GPC;
        $_GPC = Request::param(  );
        $res = [
            'status'=>0
        ];
        $this->diy_id = !empty($_GPC['diy_id']) ? $_GPC['diy_id'] : $this->diy_id;
        $res['diy_id'] = $this->diy_id;
        if( empty( $this->diy_id ) ){

            $res['status']=1;
            $res['description'] = '请求不合法';
            echo json_encode( $res );
            die();
        }

        $diy = DiyModel::single( $this->diy_id );
        $diy_arr = json_decode( $diy['diy'], true );
        foreach( $diy_arr as $key => &$item ){

            if( strpos($item['layout'],'image.')!==false ){
                foreach( $item['cubes'] as $i=>&$cube ){
                    $width = $cube['width'];
                    $height = $cube['height'];
                    // $cube['image'] = str_replace('/storage/','', $cube['image']);
                    list($image_path_pre, $image_path) = explode('/storage/', $cube['image']);

                    $cube['image'] = ImageModel::resize2($image_path, $width, $height );
                    $this->setLink( $cube );
                }

            }
            if( $item['layout'] == 'lunbo' ){
                $width = !empty($item['width'])? $item['width'] : 750;
                $height = !empty($item['height'])? $item['height'] : 360;
                foreach( $item['cubes'] as $i => &$cube ){

                    if( isset( $cube['image'] ) ){
                        list($image_path_pre, $image_path) = explode('/storage/', $cube['image']);
                        $cube['image'] = ImageModel::resize2( $image_path, $width, $height );
                        $this->setLink( $cube );
                    }

                    
                }
            }
            if( $item['layout'] == 'category' ){
                $item['cubes'] = [];
                if( !empty( $item['idArr']) ){
                    foreach( $item['idArr'] as $cat_id ){
                        $category = CategoryModel::single( $cat_id );
                        $category['link'] = '/sm_shop/pages/category/category?category_id=' . $cat_id;
                        $category['openType']='switchTab';
                        // $category['image'] =  tomedia( $category['image'] );
                        $category['image'] = ImageModel::resize2( $category['image']  );
                        $item['category'][] = $category;
                    }
                }

            }

            if( $item['layout'] == 'product' ){
                $item['cubes'] = [];
                foreach( $item['idArr'] as $product_id ){
                    $product = GoodsModel::detail( $product_id );
                    $product['link'] = '/sm_shop/pages/product/product?product_id=' . $product_id;
                    $product['openType']='switchTab';
                    // $product['image'] =  tomedia( $product['image'] );
                    $product['image'] =  ImageModel::resize2( $product['image'] );
                    $item['cubes'][] = $product;
                }
            }

            if( $item['layout'] == 'article' ){
                $item['cubes'] = [];
                foreach( $item['idArr'] as $article_id ){
                    $article = ArticleModel::detail( $article_id );
                    $article['link'] = '/sm_shop/pages/article/article?article_id=' . $article_id;

                    // $article['image'] =  tomedia( $article['image'] );
                    $article['image'] =  ImageModel::resize2( $article['image'] );
                    $article['public_time'] =  Date('Y-m-d', strtotime($article['public_time']) );
                    $item['cubes'][] = $article;
                }

            }
        }
        
        echo json_encode( $diy_arr );


    }

    public function home(Request $request ){


        $this->diy_id = SettingModel::getItem( 'diy', 'home' );
        $this->single( $request );

    }

    private function setLink( &$cube ){

        // if( !isset($cube['page_link']) || empty( $page_link) ){
        //     return;
        // }
        if( !isset($cube['page_link']) ){
            return;
        }
        $page_link = $cube['page_link'];
        $key = $page_link['key'];
        if( $key == 'product' ){
            $cube['openType']='navigate';
            $cube['link'] = '/sm_shop/pages/product/product';
            if( !empty($page_link['params']) ){
                $product_id = $page_link['params']['product_id'];
                $product_id = is_array( $product_id ) ? $product_id['value']:$product_id;
                $cube['link'] .= '?product_id=' . $product_id;
            }else{
                $cube['link'] = '/sm_shop/pages/home/home';
            }
        }
        if( $key == 'article' ){
            $cube['openType']='navigate';
            $cube['link'] = '/sm_shop/pages/article/article';
            if( !empty($page_link['params']) ){
                $article_id = $page_link['params']['article_id'];
                $article_id = is_array( $article_id ) ? $article_id['value']:$article_id;
                $cube['link'] .= '?article_id=' . $article_id;
            }else{
                $cube['link'] = '/sm_shop/pages/home/home';
            }
        }
        if( $key == 'home' ){
            $cube['openType']='switchTab';
            $cube['link'] = '/sm_shop/pages/home/home';
        }
        if( $key == 'cart' ){
            $cube['openType']='switchTab';
            $cube['link'] = '/sm_shop/pages/cart/cart';
        }
        if( $key == 'user' || $key == 'customer' ){
            $cube['openType']='switchTab';
            $cube['link'] = '/sm_shop/pages/user/user';
        }
        if( $key == 'order' ){
            $cube['openType']='navigate';
            $cube['link'] = '/sm_shop/pages/order/list';
        }
        if( $key == 'category' ){
            $cube['openType']='switchTab';
            $cube['link'] = '/sm_shop/pages/category/category';
            $link_params = [];

            if( $page_link['params']['category_id']['key']=='category_id' ){
                $link_params[] = 'category_id=' . $page_link['params']['category_id']['value'];
            }
            if( $page_link['params']['category_id']['key']=='brand_id' ){
                $link_params[] = 'brand_id=' . $page_link['params']['category_id']['value'];
            }
            if( !empty( $link_params ) ){
                $cube['link'] .= '?' . implode('&', $link_params );
            }
        }
        if( $key == 'box' ){
            $cube['openType']='navigate';
            $cube['link'] = '/sm_shop/pages/box/box';
        }

        if( $key == 'free' ){
            $cube['openType']='navigate';
            $cube['link'] = $page_link['params']['path']['value'];
        }

    }
}